package org.dromara.tearoom.service.impl;

import com.google.common.collect.Maps;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.tearoom.domain.bo.TerHornLoginBo;
import org.dromara.tearoom.domain.vo.TerHornLoginVo;
import org.dromara.tearoom.mapper.TerHornLoginMapper;
import org.dromara.tearoom.service.ISYWNService;
import org.dromara.tearoom.service.ITerHornLoginService;
import org.springframework.stereotype.Service;
import org.dromara.tearoom.domain.bo.TerHornEquipmentBo;
import org.dromara.tearoom.domain.vo.TerHornEquipmentVo;
import org.dromara.tearoom.domain.TerHornEquipment;
import org.dromara.tearoom.mapper.TerHornEquipmentMapper;
import org.dromara.tearoom.service.ITerHornEquipmentService;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 喇叭设备管理Service业务层处理
 *
 * @author xch
 * @date 2025-04-23
 */
@RequiredArgsConstructor
@Service
public class TerHornEquipmentServiceImpl implements ITerHornEquipmentService {

    private final TerHornEquipmentMapper baseMapper;
    private final ISYWNService sYWNService;
    private final ITerHornLoginService terHornLoginService;
    /**
     * 查询喇叭设备管理
     *
     * @param id 主键
     * @return 喇叭设备管理
     */
    @Override
    public TerHornEquipmentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询喇叭设备管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 喇叭设备管理分页列表
     */
    @Override
    public TableDataInfo<TerHornEquipmentVo> queryPageList(TerHornEquipmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TerHornEquipment> lqw = buildQueryWrapper(bo);
        Page<TerHornEquipmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的喇叭设备管理列表
     *
     * @param bo 查询条件
     * @return 喇叭设备管理列表
     */
    @Override
    public List<TerHornEquipmentVo> queryList(TerHornEquipmentBo bo) {
        LambdaQueryWrapper<TerHornEquipment> lqw = bo!=null?buildQueryWrapper(bo): Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TerHornEquipment> buildQueryWrapper(TerHornEquipmentBo bo) {
        LambdaQueryWrapper<TerHornEquipment> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TerHornEquipment::getId);
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), TerHornEquipment::getDeviceName, bo.getDeviceName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceNo()), TerHornEquipment::getDeviceNo, bo.getDeviceNo());
        lqw.eq(StringUtils.isNotBlank(bo.getDescriptionAuto()), TerHornEquipment::getDescriptionAuto, bo.getDescriptionAuto());
        lqw.eq(StringUtils.isNotBlank(bo.getDescriptionIdly()), TerHornEquipment::getDescriptionIdly, bo.getDescriptionIdly());
        lqw.eq(bo.getTeroomId() != null, TerHornEquipment::getTeroomId, bo.getTeroomId());
        lqw.eq(bo.getDeviceSpd() != null, TerHornEquipment::getDeviceSpd, bo.getDeviceSpd());
        lqw.eq(bo.getDevicePit() != null, TerHornEquipment::getDevicePit, bo.getDevicePit());
        lqw.eq(bo.getDeviceVol() != null, TerHornEquipment::getDeviceVol, bo.getDeviceVol());
        lqw.eq(bo.getDevicePer() != null, TerHornEquipment::getDevicePer, bo.getDevicePer());
        return lqw;
    }

    /**
     * 新增喇叭设备管理
     *
     * @param bo 喇叭设备管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TerHornEquipmentBo bo) {
        bo.setDelFlag("0");
        TerHornEquipment add = MapstructUtils.convert(bo, TerHornEquipment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改喇叭设备管理
     *
     * @param bo 喇叭设备管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TerHornEquipmentBo bo) {
        TerHornEquipment update = MapstructUtils.convert(bo, TerHornEquipment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TerHornEquipment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除喇叭设备管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
    @Override
    public Boolean idlyBroadcast(TerHornEquipmentBo bo, String type) {
        TerHornLoginBo hlbo = new TerHornLoginBo();
        hlbo.setDeviceId(bo.getId());
        TerHornLoginVo  tvo  =null;
        LocalDateTime now = LocalDateTime.now();

        if(StringUtils.isNotBlank(bo.getCookieValue())){
            tvo = new TerHornLoginVo();
            tvo.setCookieValue(bo.getCookieValue());
            tvo.setModifyTime(bo.getModifyTime());
        }else{
            tvo=  terHornLoginService.queryByDeviceId(hlbo);
        }
        if(tvo!=null){
            Duration duration = Duration.between(tvo.getModifyTime(), now);
            if(duration.toHours()> 48){
                hlbo.setCookieValue(sYWNService.sYWNLogin());
                hlbo.setModifyTime(now);
               if(terHornLoginService.updateByBo(hlbo)<1)return false;
            }else {
                hlbo.setCookieValue(tvo.getCookieValue());
            }
        }else{
            hlbo.setCookieValue(sYWNService.sYWNLogin());
            hlbo.setModifyTime(now);
            hlbo.setDeviceId(bo.getId());
            terHornLoginService.insertByBo(hlbo);
        }

        Map<String, Object> params = Maps.newHashMap();
        if(type!=null&&type.equals("auto")){
            params.put("text", bo.getDescriptionAuto());
        }else{
            params.put("text", bo.getDescriptionIdly());
        }
        params.put("spd", bo.getDeviceSpd());
        params.put("pit", bo.getDevicePit());
        params.put("vol", bo.getDeviceVol());
        params.put("per", bo.getDevicePer());
        return sYWNService.sendSpeakerText(bo.getDeviceNo(),hlbo.getCookieValue(),params);
    }
    @Override
    public List<TerHornEquipmentVo> selectEquipmentAndCookie(){
        return baseMapper.selectEquipmentAndCookie();
    }
}
